﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufItem.h"
#include "GnufButtonControl.h"

namespace gnuf {
    
    class ButtonAppearanceSetting;
    class QtGrid;
    class Block;

    class GNUF_BASE_EXPORT Grid final
        : public ItemWith<QtGrid>
    {
        CANNOT_COPY_OR_ASSIGN(Grid);
        
    public:
        static const EItemType Type;
        explicit Grid(Block *);
        virtual ~Grid();
        template<typename TItem>
        TItem* addItem()
        {
            static_assert(std::is_base_of<Item, TItem>::value, "typename just only inherit from Item");
            gcmp::OwnerPtr<TItem> item = NEW_AS_OWNER_PTR(TItem, this);
            TItem* ptr = item.get();
            m_contents.push_back(TransferOwnership(item));
            return ptr;
        }

        template<typename TItem>
        TItem* addItem(gcmp::OwnerPtr<TItem>&& item)
        {
            static_assert(std::is_base_of<Item, TItem>::value, "typename just only inherit from Item");
            TItem* ptr = item.get();
            m_contents.push_back(TransferOwnership(item));
            return ptr;
        }
        gcmp::OwnerPtr<ButtonAppearanceSetting> createCustomizedBtnSetting(bool showButtonCaption = true);
        bool hasContents() const { return !m_contents.empty(); }
        void dumpContents();

        typedef std::list< gcmp::OwnerPtr<Item> > Contents;
        typedef std::vector< Item * > RawContents;
        const Contents & contents() const { return m_contents; }
        RawContents rawContents();

    protected:

        // Inherits from ItemWith...
        virtual void onDisconnectFromMyQtItem() override;
        virtual void onPostAppearanceSettingChanged() override;
        virtual void onPostVisibilityChanged() override;

    private:
        
        void refreshIcon(bool showButtonCaption);
        Grid::Contents m_contents;
    };
}
